{% extends 'admin/index.html' %}

{% block con-title %}
    <div class="container-fluid am-cf">
        <div class="row">
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-9">
                <div class="page-header-heading">
                    <span class="am-icon-typess page-header-heading-icon"></span>
                     分类列表
                   </div>
            </div>
          
        </div>
    </div>
{% endblock %}

{% block con-body %}
<div class="row-content am-cf">
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <div class="widget-head am-cf">
                    <div class="widget-title  am-cf">分类列表</div>
                </div>
                <div class="widget-body  am-fr">

                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                        <div class="am-form-group">
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button type="button" class="am-btn am-btn-default am-btn-success"><span class="am-icon-plus"></span> 新增</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-secondary"><span class="am-icon-save"></span> 保存</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-warning"><span class="am-icon-archive"></span> 审核</button>
                                    <button type="button" class="am-btn am-btn-default am-btn-danger"><span class="am-icon-trash-o"></span> 删除</button>
                                </div>
                            </div>
                        </div>
                    </div>

                <form action="" method="get">
                    <div class="am-u-sm-12 am-u-md-6 am-u-lg-3">
                        <div class="am-form-group tpl-table-list-select">
                            <select name="type" data-am-selected="{btnSize: 'sm'}" style="display: none;">
                              <option {% if request.GET.type == 'name' %} selected {% endif %} value="name">分类名</option>

                            </select>
                        </div>
                    </div>
                    <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
                        <div class="am-input-group am-input-group-sm tpl-form-border-form cl-p">
                            <input name="keywords" value="{{ request.GET.keywords }}" type="text" class="am-form-field ">
                            <span class="am-input-group-btn">
                                <button class="am-btn  am-btn-default am-btn-success tpl-table-list-field am-icon-search" ></button>
                              </span>
                        </div>
                    </div>
                </form>



                    <div class="am-u-sm-12">
                        <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black ">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>分类名</th>
                                    <th>所属分类</th>
                                    <th>分类路径</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>

                            {% for v in typelist %}
                            <tr class="gradeX">
                                <td class="am-text-middle">{{ v.id }}</td>
                                <td class="am-text-middle editnsme" tid="{{ v.id }}">
                                    {% if v.pid != 0 %}
                                        &nbsp;&nbsp;<span>|----</span>
                                    {% endif %}


                                    <span class='name'>{{ v.name }}</span>

                                </td>
                                <td class="am-text-middle">{{ v.pname|default:'一级分类' }}</td>
                                <td class="am-text-middle">{{ v.path }}</td>
                                
                                <td class="am-text-middle">
                                    <div class="tpl-table-black-operation">
                                        <a href="javascript:;" sid="{{ v.id }}"  class="tpl-table-black-operation-del">
                                            <i class="am-icon-trash"></i> 删除
                                        </a>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                         
                                <!-- more data -->
                            </tbody>
                        </table>
                    </div>
                    <div class="am-u-lg-12 am-cf">

                        <div class="am-fr">
                            <ul class="am-pagination tpl-pagination">

                            {% load pagetag %}

                            {% showpage typelist.paginator.num_pages request %}                            


                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    // 动态修改分类名

    // 给分类名绑定双击事件
    $('.editnsme').dblclick(function(){
        // 先获取当前的分类名
        var name = $(this).find('.name').text()
        var t = $(this)
        // 把td中的分类名的内容换成一个 input
        var inp = $('<input type="text" value="'+name+'" style="color:black;" />')
        $(this).find('.name').html(inp)
        // 设置当前input的内容直接选中
        inp.select()
        // 给input绑定丧失焦点事件
        inp.blur(function(){
            // 获取当前的值
            var newname = $(this).val()
            // 判断是否更新了分类名
            if(newname == name){
                // 恢复原样
                $(this).remove();//删除input
                t.find('.name').text(name)
            }else{
                // 获取分类id newname id
                var tid = t.attr('tid')

                //发送ajax,修改数据
                $.get('{% url "myadmin_types_update" %}',{id:tid,name:newname},function(data){
                    console.log(data)
                    t.find('.name input').remove()
                    if(data['code'] == 0){
                        // 成功,设置新的分类名
                        t.find('.name').text(newname)
                    }else{
                        // 恢复原样
                        t.find('.name').text(name)
                    }
                },'json')
            }
        })
    })    


    // 给删除的按钮 绑定单击事件
    $('.tpl-table-black-operation-del').click(function(){
        // 获取id
        var id = $(this).attr('sid')
        var btn = $(this)

        // 发送ajax请求,执行数据的删除
        $.get('{% url "myadmin_types_delete" %}',{id:id},function(data){
            if(data['code'] == 0){
                // 在页面中 移除当前这条数据
                btn.parents('tr').remove()
            }
            alert(data['msg'])
        },'json')
    })


</script>


{% endblock %}
